from os import path
import re
num = {}
with open('sample.txt') as f:
    for i in re.split('[^a-z&A-Z]',f.read()):
        if i:
            num[i.lower()] = num.setdefault(i.lower(), 0) + 1
print(sorted(num.items(),key = (lambda item:item[1]),reverse = True)[:10])


# 大文件的会占用大量内存，可以优化一下

#修改如下
from os import path
import re
num = {}
j = "s"
with open('sample.txt') as f:
    while j:
        j = f.readline()
        i = re.split('[^a-z&A-Z]',j)
        for k in i:
            if k:
                num[k.lower()] = num.setdefault(k.lower(), 0) + 1
print(sorted(num.items(),key = (lambda item:item[1]),reverse = True)[:10])